//
//  HGApp.m
//  HGMM
//
//  Created by 2008-bj5 on 15/8/4.
//  Copyright (c) 2015年 cc. All rights reserved.
//

#import "HGApp.h"
#import "HGPluginViewController.h"
#import "SVProgressHUD.h"
//#import "HGRequestUtil.h"
#import "UIView+ViewController.h"
#import "DataSave.h"
#import "AppDelegate.h"
#import "MBProgressHUD+LCQ.h"


@implementation HGApp

- (void)show:(CDVInvokedUrlCommand*)command{
    CCLog(@"HGApp - show");
    
    UIAlertController *alterVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"HGApp - show在项目中无对应方法" preferredStyle:(UIAlertControllerStyleAlert)];
    [alterVC addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {

    }]];

    [self.webView.viewContoller presentViewController:alterVC animated:YES completion:nil];
    
}

- (void)back:(CDVInvokedUrlCommand*)command{
    CCLog(@"HGApp - back");
    HGPluginViewController *vc = (HGPluginViewController *)self.viewController;
    UIWebView *webView = (UIWebView *)vc.webView;
    if ([webView canGoBack])
    {
        
        [webView goBack];
    }
    else{
        
        UIAlertController *alterVC = [UIAlertController alertControllerWithTitle:nil message:@"页面不可以再回退" preferredStyle:(UIAlertControllerStyleAlert)];
        [alterVC addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        
        [self.webView.viewContoller presentViewController:alterVC animated:YES completion:nil];
        
    }
    
    

}

- (void)close:(CDVInvokedUrlCommand*)command{
    CCLog(@"HGApp - close");
    HGPluginViewController *vc = (HGPluginViewController *)self.viewController;
    [vc.navigationController popViewControllerAnimated:YES];
    
}

- (void)clearCache:(CDVInvokedUrlCommand*)command{
    CCLog(@"HGApp - clearCache");
     
     [((AppDelegate *)[UIApplication sharedApplication].delegate) clearPluginData];
    [MBProgressHUD showSuccess:@"清理插件缓存成功" image:nil];
    
}


- (void)forward_xhw:(CDVInvokedUrlCommand*)command {
    __block CDVPluginResult *pluginResult = nil;
    if (command.arguments.count !=2 || ![command.arguments[0] isKindOfClass:[NSString class]] || ![command.arguments[1] isKindOfClass:[NSString class]]) {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"error params!"];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        return ;
    }
    
    NSString *XHW = @"XHW";
    if (![XHW isEqualToString:command.arguments[0]]) {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[NSString stringWithFormat:@"不是可信任的参数: %@", command.arguments[0]]];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        return ;
    }
    
    if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"CLearning://normal/"]]) {


        
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:@"请安装学海网App，如何下载安装请联系相关部门！" preferredStyle:(UIAlertControllerStyleAlert)];
        [alertVC addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
        
        }]];
        [self.webView.viewContoller presentViewController:alertVC animated:YES completion:^{
            
        }];
        
        
        return ;
    }
    
    /*
     @property(nonatomic,copy)NSString *guid;   //guid
     @property(nonatomic,copy)NSString *usercoder;  //官员号
     @property(nonatomic,copy)NSString *username;   //用户名称
     @property(nonatomic,copy)NSString *dept; //单位名称
     */
    
    DataSave *dataSava = [DataSave shareInstance];
    UserModel *model = [dataSava getUserInfo];
    NSString *GUID = model ? model.guid : @"";
    NSString *usercoder = model ? model.usercoder : @"";
    NSString *userName = model ? model.username : @"";
    NSString *dept = model ? model.dept : @"";
    
    
    
    NSString *urlString = [NSString stringWithFormat:@"CLearning://normal/?guid=%@&usercode=%@&username=%@&dept=%@", GUID, usercoder, userName, dept];
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0 ) {
        
        urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    }
    
    
   
    
    NSURL *url = [NSURL URLWithString:urlString];
 
    if (@available(iOS 10.0, *)) {
            [[UIApplication sharedApplication]openURL:url options:@{} completionHandler:nil];
    } else {
        
            [[UIApplication sharedApplication] openURL:url];
    }

    
}


@end
